package com.example.goodpet.base;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.goodpet.R;
import com.example.goodpet.activity.adapter.MyFragmentVPAdapter;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.UIUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.List;

/**
 * vp管理fragment 的activity
 */
public abstract class VPMFragmentActivity extends FragmentActivity {
    public TextView cmTitleText;
    public ImageView cmTitleLeft;
    public TextView cmTextRight;
    public ViewPager2 vpInfo;
    private TabLayout tabLayout;
    private TabLayoutMediator mediator;

    public List<Fragment> fragmentList;

    public String TAG=this.getClass().getName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_release);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        cmTitleLeft=findViewById(R.id.cmTitleLeft);
        cmTitleText=findViewById(R.id.cmTitleText);
        cmTextRight=findViewById(R.id.cmTitleRight);
        tabLayout=findViewById(R.id.tabLayout);
        vpInfo=findViewById(R.id.vpInfo);
        
        setFragmentList();

        int position = getIntent().getIntExtra("position", 0);
        vpInfo.setAdapter(new MyFragmentVPAdapter(getSupportFragmentManager(),getLifecycle(),fragmentList));
        vpInfo.setCurrentItem(position);

        mediator=new TabLayoutMediator(tabLayout, vpInfo, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(UIUtils.getStringArr(setTabTitle())[position]);
            }
        });
        mediator.attach();

        initListener();
        initTitle();
    }

    protected abstract void initListener();

    /**
     * 
     * @return 返回R.array.id
     */
    protected abstract int setTabTitle();

    protected abstract void initTitle();

    /**
     * 为fragmentList变量初始化及设置数据
     */
    public abstract void setFragmentList();

}
